<template>
	<view class="content">
		<view class="uni-list">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, index) in news" :key="index" @tap="openinfo" :data-newsid="item.post_id">
				<view class="uni-media-list">
					<image class="uni-media-list-logo" :src="item.img"></image>
					<view class="uni-media-list-body">
						<view class="uni-media-list-text-top">{{ item.title }}</view>
						<view class="uni-media-list-text-bottom uni-ellipsis">{{ item.content }}</view>	
					</view>
				</view>
			</view>		
		</view>	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				news: [] // 确保这里的变量名与v-for中使用的一致
			};
		},
		onLoad: function() {
			uni.request({
				url: 'https://unidemo.dcloud.net.cn/api/news', // 移除了HTML实体编码
				method: 'GET',
				data: {},
				success: res => {
					console.log(res);
					this.news = res.data; // 确保这里的变量名与data中定义的一致
				},
				fail: () => {
					// 添加错误处理逻辑，例如提示用户
					uni.showToast({
						title: '加载新闻失败',
						icon: 'none'
					});
				},
				complete: () => {
					// 如果需要，添加完成后的处理逻辑
				}
			});
		},
		methods:{
			openinfo(e){
				var newsid = e.currentTarget.dataset.newsid;
				console.log(newsid);
				uni.navigateTo({
					url: `../info/info?newsid=${newsid}` // 使用模板字符串
				});
			}	
		}
	};
</script>

<style>
.uni-media-list-body { height: auto; }
.uni-media-list-text-top { line-height: 1.6em; }
</style>